package com.mxf.springcloud.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author : [mxf]
 * @version : [v1.0]
 * @className : MyLogGatewayfilter
 * @description : [描述说明该类的功能]
 * @createTime : [2022/10/3 23:55]
 * @updateUser : [mxf]
 * @updateTime : [2022/10/3 23:55]
 * @updateRemark : [描述说明本次修改内容]
 */
@Component
@Slf4j
public class MyLogGatewayfilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String uname = exchange.getRequest().getQueryParams().getFirst("uname");
        if (uname != null) {
            return chain.filter(exchange);
        }
        log.error("非法用户，uname为null");
        exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
        return exchange.getResponse().setComplete();
    }

    @Override
    public int getOrder() {
        // 数字越小优先级越高
        return 0;
    }
}
